

// import shopJson from 'shopCartDataJson.js';


var money = 0,count = 0;

Page({
  selectedMenuId: 1,  
  data: {
    'getdata': '',
    'total': {
      'count': count,
      'money': money,
      'checkBox': false
    }  
  },
  handleTap1:function(e){
    let that = this;
    let getdata = this.data.getdata;
    let dataset = e.currentTarget.dataset;
    let storeid = e.currentTarget.dataset.storeid;
    let viewid = e.currentTarget.dataset.viewid;
    wx.showModal({
      title: '',
      content: '您确定删除此商品吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          let storeId = getdata.find(function (v) {
            return v.id == storeid;
          });
          let deteleId = storeId.shopCartData.find(function (v) {
            return v.id == viewid;
          })
          this.setData({
            getdata: getdata
          });
            // let carts = this.data.carts;
            // carts.splice(index, 1);              // 删除购物车列表里这个商品
            // this.setData({
            //   carts: carts
            // });
            // if (!carts.length) {                  // 如果购物车为空
            //   this.setData({
            //     hasList: false              // 修改标识为false，显示购物车为空页面
            //   });
            // } else {                              // 如果不为空
            //   // this.getTotalPrice();           // 重新计算总价格
            // }

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  addCount:function (event) {
    let data = event.currentTarget.dataset;
    let total = this.data.total;
    let getdata = this.data.getdata;
    // console.log(total);
    // console.log(getdata);
    let shopCartDataItem = getdata.data.find(function (v) {
      return v.shop_goods_id == data.id;
    })
    shopCartDataItem.goods_cart_counts += 1;
    if (shopCartDataItem.checked === true){
      total.money = (total.money*1000 + shopCartDataItem.sale_price*1000)/1000;
      total.count += 1;
    }
    this.setData({
      'getdata': getdata,
      'total': total
    })
  },
  minusCount:function (event) {
    let data = event.currentTarget.dataset;
    let total = this.data.total;
    let getdata = this.data.getdata;

    let shopCartDataItem = getdata.data.find(function (v) {
      return v.shop_goods_id == data.id;
    })
    if (shopCartDataItem.goods_cart_counts <= 1)
      return
    shopCartDataItem.goods_cart_counts -= 1;
    if (shopCartDataItem.checked === true) {
      total.money = (total.money * 1000 - shopCartDataItem.sale_price * 1000) / 1000;
      total.count -= 1;
    }   
    this.setData({
      'getdata': getdata, 
      'total': total
    })
  },
  storeContaienrTap:function(event){
    let total = this.data.total;
    let data = this.data.getdata;
    let checkTapStatus = event.currentTarget.dataset.checked;
    let checkId = event.currentTarget.dataset.checkid;
    let selectFooterCheck = true; 
    if (checkTapStatus === false){
        let checkedPrice = 0;
        let checkedAllPrice = 0;
        let storeCount = 0;
        let storeCheckCount = 0;
        data.checked = true;
        console.log(data)
        for (let i = 0; i < data.data.length ; i++){
          if (data.data[i].checked === true) {
            checkedPrice += ((data.data[i].sale_price*1000)/1000) * ((data.data[i].goods_cart_counts)*1000)/1000; 
            storeCheckCount += data.data[i].goods_cart_counts; 
          } 
          storeCount += data.data[i].goods_cart_counts;
          checkedAllPrice += ((data.data[i].sale_price * 1000) / 1000) * ((data.data[i].goods_cart_counts) * 1000)/1000;        
          data.data[i].checked = true;
        }
          for (let l = 0; l < data.data.length; l++) {
            if (data.data[l].checked === false) {
              selectFooterCheck = false;
            }
          }
        total.checkBox = selectFooterCheck;        
        total.money -= checkedPrice;
        total.money += checkedAllPrice;
        total.count -= storeCheckCount;
        total.count += storeCount;
    }
    if (checkTapStatus === true) {
      let checkedPrice = 0;
      let checkedAllPrice = 0;
      let storeCount = 0;     
      data.checked = false;
      total.checked = false;
      for (let i = 0; i < data.data.length; i++) {
        data.data[i].checked = false;
        checkedAllPrice += ((data.data[i].sale_price * 1000) / 1000) * ((data.data[i].goods_cart_counts) * 1000) / 1000; 
        storeCount += data.data[i].goods_cart_counts; 
      }
        total.money -= checkedAllPrice;
        total.count -= storeCount;
        total.checkBox = false;
    }
    this.setData({
      'getdata': data,
      'total': total
    })
  },
  footerCheckBoxTap(event){
    let dataset = event.currentTarget.dataset;
    var data = this.data.getdata;
    let dataChecked = dataset.checkbox;
    let dataTotal = this.data.total;
    dataTotal.count = 0;
    if (dataChecked === false){
      let checkedAllPrice = 0;
      let storeAllCount = 0;      
      data.checked = true;
      for (var j = 0; j < data.data.length;j++){
        data.data[j].checked = true;
        checkedAllPrice += ((data.data[j].sale_price * 1000) / 1000) * ((data.data[j].goods_cart_counts) * 1000) / 1000; 
        storeAllCount += data.data[j].goods_cart_counts; 
      }
      dataTotal.checkBox = true;
      dataTotal.money = checkedAllPrice;
      dataTotal.count = storeAllCount;
    }
    if (dataChecked === true) {    
        data.checked = false;
        for (var j = 0; j < data.data.length; j++) {
          data.data[j].checked = false;
        }
      dataTotal.money = 0;
      dataTotal.count = 0;
      dataTotal.checkBox = false;
    }
    console.log(data)
    this.setData({
      'getdata': data,
      'total': dataTotal
    })
  },
  checkboxTap: function (event){
    let data = this.data.getdata;
    let dataset = event.currentTarget.dataset;
   
    let listId = dataset.id;
    let checkbox = dataset.checked;
    let selectCheck = true;
    let selectFooterCheck = true;
    let dataTotal = this.data.total;
    if (checkbox === false){  //点击勾选
      console.log(data)
      let shopCartDataItem = data.data.find(function (v) {
        return v.shop_goods_id == listId;
      })
      console.log('shopCartDataItem', shopCartDataItem)
      shopCartDataItem.checked = true;
      for (let i = 0; i < data.data.length; i++) {
        if (data.data[i].checked === false) {
          selectCheck = false;
        }
      } 
        for (let l = 0; l < data.data.length; l++){
          if (data.data[l].checked === false) {
            selectFooterCheck = false;
          }
        }    
      data.checked = selectCheck;
      dataTotal.checkBox = selectFooterCheck;
      dataTotal.money += ((shopCartDataItem.sale_price*1000)/1000) * ((shopCartDataItem.goods_cart_counts*1000)/1000);
      dataTotal.count += shopCartDataItem.goods_cart_counts;
    }
    if (checkbox === true) {  //取消勾选

      data.checked = false;
      let shopCartDataItem = data.data.find(function (v) {
        return v.shop_goods_id == listId;
      })
      dataTotal.checkBox = false;
      shopCartDataItem.checked = false;
      dataTotal.money -= shopCartDataItem.sale_price * shopCartDataItem.goods_cart_counts;
      dataTotal.count -= shopCartDataItem.goods_cart_counts;
    }   
    this.setData({
      'getdata': data,
      'total': dataTotal
    })    
  },
  onReady: function () {
    var that = this;
    var data = this.data;
    var shopCartData = new Object;
    wx.request({
      url: 'http://ptd.china-mail.com.cn/putaotang/api/v1/goods/lookShoppingCart', //接口地址
      // url: 'https://ptd.china-mail.com.cn/{wx8965cee14d79c027}/{0}/putaotang/api/v1/goods/lookShoppingCart', //接口地址
      data: {
        token: '38a55154ef8a1a400db888dca5222640ac'
      },
      method: 'GET',
      dataType: 'json',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('suc', res)
        shopCartData = res.data;
        console.log('shopCartDatashopCartData', shopCartData)
        // for (let i = 0; i < shopCartData.length; i++) {
          shopCartData.checked = false;
          if (shopCartData.checked === false) {
            for (let j = 0; j < shopCartData.data.length; j++) {
              shopCartData.data[j].checked = false;
              shopCartData.data[j].pic = 'http://ptd.china-mail.com.cn' + shopCartData.data[j].pic.split(',')[0];
              if (shopCartData.data[j].platform_goods_spec_names === undefined){

              }else{
                shopCartData.data[j].platform_goods_spec_names = shopCartData.data[j].platform_goods_spec_names.split(',');
                shopCartData.data[j].platform_goods_spec_value_names = shopCartData.data[j].platform_goods_spec_value_names.split(',');
              }
              if (shopCartData.data[j].checked === true) {
                money += shopCartData.data[j].sale_price * shopCartData.shopCartData[j].goods_cart_counts;
                count += shopCartData.data[j].goods_cart_counts;
              }
            }
          }
          if (shopCartData.checked === true) {
            for (let j = 0; j < shopCartData.data.length; j++) {
              shopCartData.data[j].checked = false;
              money += shopCartData.data[j].sale_price * shopCartData.data[j].goods_cart_counts;
              count += shopCartData.data[j].goods_cart_counts;
            }
          }
        // }

        that.setData({
          'getdata': shopCartData,
          'total': {
            'count': count,
            'money': money,
            'checkBox': false
          }
        }) 

        console.log('thisdata',that.data) 
      },
      fail: function (err) {
        console.log('err', err);
      }, complete: function (data) {
        
      }
    })

  }
})